package tk.leafotto.sign2.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CalendarView;

import tk.leafotto.sign2.R;

public class CalendarActivity extends Activity {

    private CalendarView calendarView;
    private String date;
    private boolean sdkUpX;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_calendar);
        sdkUpX = Build.VERSION.SDK_INT > 21?true:false;
        initUI();
    }

    private void initUI() {
        calendarView = (CalendarView) findViewById(R.id.calendarView);
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                String monStr = month < 9 ? "0" + (month + 1) : month + 1 + "";
                String dayStr = dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth + "";
                date =  year + "-" + monStr + "-" + dayStr;
                if(sdkUpX){
                    submit();
                }
            }
        });

        Button bt_submit = (Button) findViewById(R.id.bt_submit);
        if(sdkUpX){
            bt_submit.setVisibility(View.GONE);
        }
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void submit(){
        Intent intent = new Intent();
        intent.putExtra("date",date);
        setResult(10,intent);
        finish();
    }
}
